package com.example.hfweather.ui.splash

import android.annotation.SuppressLint
import android.content.Intent
import android.os.Bundle
import android.widget.Toast
import androidx.activity.enableEdgeToEdge
import androidx.appcompat.app.AppCompatActivity
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat
import com.example.hfweather.MainActivity
import com.example.hfweather.R
import com.example.hfweather.storage.TokenPreferences
import com.example.hfweather.ui.login.LoginActivity

@SuppressLint("CustomSplashScreen")
class SplashActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        checkTokenAndRedirect()

    }
    private fun checkTokenAndRedirect() {
        try {
            val token= TokenPreferences.getInstance(this).getToken()
            if (token.isNullOrEmpty()) {
                startActivity(Intent(this, LoginActivity::class.java))

            } else {
                startActivity(Intent(this, MainActivity::class.java))

            }
            finish()

        }catch (e: Exception){
            e.printStackTrace()
            Toast.makeText(this, "请先登录", Toast.LENGTH_SHORT).show()
            val intent = Intent(this, LoginActivity::class.java)
            startActivity(intent)
            finish()
        }

    }
}